/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.biblioteca.bl.AutorBlLocal;
import mx.its.dsw2.biblioteca.bl.CategoriaBlLocal;
import mx.its.dsw2.biblioteca.bl.LibroBlLocal;
import mx.its.dsw2.biblioteca.modelo.Categoria;
import mx.its.dsw2.biblioteca.modelo.Libro;

/**
 *
 * @author lesly
 */
@ManagedBean
@SessionScoped
public class BookBean{

    @EJB
    private CategoriaBlLocal categoriaBl;
    @EJB
    private AutorBlLocal autorBl;
    @EJB
    private LibroBlLocal libroBl;
    private List<Libro> lista;
    private List<Categoria> listaCate;
    private Libro book;
    private Categoria category;
    private int authorSelect;
    private int categorySelect;
    private String accion = " ";
     private Date date3;  
    

    /**
     * Creates a new instance of BookBean
     */
    public BookBean() {
        book = new Libro();
    }

    public Date getDate3() {
        return date3;
    }

    public void setDate3(Date date3) {
        this.date3 = date3;
    }

    public List<Categoria> getListaCate() {
        return listaCate;
    }
    

    public List<Libro> getLista() {
        lista = libroBl.getAll();
        return lista;
    }

    public int getCategorySelect() {
        return categorySelect;
    }

    public void setCategorySelect(int categorySelect) {
        this.categorySelect = categorySelect;
    }

    public Libro getBook() {
        return book;
    }

    public void setBook(Libro book) {
        this.book = book;
    }

    public int getAuthorSelect() {
        return authorSelect;
    }

    public void setAuthorSelect(int authorSelect) {
        this.authorSelect = authorSelect;
    }
    //--------------ACCION-------------------

    public String procesarSeleccion() {
        book.setIdAutor(autorBl.findById(authorSelect));
        return "BookConfirm";
    }

    public String procesarRegresar() {
        if (accion.equalsIgnoreCase("Nuevo") || accion.equalsIgnoreCase("Editar")) {
            return "BookCreateEdit";
        } else {
            return "BookList";
        }
    }

    public void prepararEditar(ActionEvent evt) {
        accion = "Editar";
//        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
//        book = lista.get(index);
        listaCate=categoriaBl.getListByBook(book);
    }

    public void prepararEliminar(ActionEvent evt) {
        accion = "Eliminar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        book = lista.get(index);
    }
//     public void prepararEliminarCate(ActionEvent evt) {
//        accion = "Eliminar";
//        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
//        category=listaCate.get(index);
//    }

    public String proceesarAccion() {
        if (accion.equalsIgnoreCase("Nuevo")) {
            libroBl.register(book, listaCate);
        } else {
            if (accion.equalsIgnoreCase("Editar")) {
                libroBl.modify(book,listaCate);
            } else {
                if (accion.equalsIgnoreCase("Eliminar")) {
                    libroBl.delete(book);
                }
            }
        }
        accion = " ";
        return "BookList";
    }
    //LISTENERS

    public void prepararNuevo(ActionEvent evt) {
        listaCate = new ArrayList<Categoria>();
        accion = "Nuevo";
        book = new Libro();
    }
    public void agregarCategoria(ActionEvent evnt){
    listaCate.add(categoriaBl.findById(categorySelect));
    }
//      public void save(ActionEvent actionEvent) {  
//        FacesContext context = FacesContext.getCurrentInstance();  
//        context.addMessage(null, new FacesMessage("Second Message", "Additional Info Here..."));  
//    } 
}